//
//  LTTicketController.m
//  LocalTravel
//
//  Created by zheng ji on 14-3-19.
//  Copyright (c) 2014年 jxw. All rights reserved.
//
#define  ticketTableWidth 256
#define  frameWidth 800
#define frameHeight 700
#import "LTTicketController.h"

@interface LTTicketController ()
{
    NSString* _sceneryId;
    NSArray* _priceArray;
    UITableView* _tableTicket;
    NSDictionary* _selectedTicketDict;
    TicketOrderView* _detailView;
    UIActivityIndicatorView *_activityView;
    UILabel* _loadMoreTip;
    NSMutableDictionary* _dictAllPolicy;
}
@end

@implementation LTTicketController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    CGRect  frame= CGRectMake(0, 0, frameWidth, frameHeight);
    UINavigationController* navi= self.navigationController;
    
    navi.view.superview.frame =frame;
    navi.view.superview.center =  CGPointMake(land_full_width/2-70, land_full_height/2-40);
    
    
     [self initUI];
 

    
   
}
-(void)viewDidAppear:(BOOL)animated{
    [self loadTicketData];

}
-(void)closeController:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)initUI{
    self.navigationItem.title=@"门票预订";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closeController:)];
    _tableTicket=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, ticketTableWidth, frameHeight-50) style:UITableViewStylePlain];
    _tableTicket.delegate=self;
    _tableTicket.dataSource=self;
    [_tableTicket registerClass:[TicketTableCell class] forCellReuseIdentifier:@"TicketTableCell"];
    [self.view addSubview:_tableTicket];
    
    
    UIView* viewRight=[[UIView alloc]initWithFrame:CGRectMake(ticketTableWidth+2, 0, frameWidth-ticketTableWidth , frameHeight)];
    viewRight.backgroundColor=[UIColor whiteColor];
    viewRight.layer.borderWidth=1;
    
    CGRect detailViewFrame=CGRectMake(10, 10, frameWidth-ticketTableWidth-20 , frameHeight-20);
     _detailView=[[TicketOrderView alloc] initWithFrame:detailViewFrame];
    _detailView.backgroundColor=RGB(245,245,245);
     _detailView.dictScenery=self.dictScenery;
    _detailView.delegate=self;
    
    [viewRight addSubview:_detailView];
    [self.view addSubview:viewRight];
    
 
    
    [self createActivityIndicatorView];
    
    
    
}

//提交订单
-(void)submitOrder:(NSDictionary*)dictRequest{
    [self createActivityIndicatorView];
    _detailView.btnSubmitOrder.enabled=NO;
    _detailView.btnSubmitOrder.titleLabel.text=@"正在处理";
    
    NSDictionary* dictReturn=  [SceneryOrderRequest submitOrder:dictRequest];
    
    NSString* rspCode=[dictReturn objectForKey:@"rspCode" ];
    if(rspCode !=nil && [rspCode isEqualToString:@"0000"]){
        //成功
        _detailView.lblResultTip.text=@"预订成功，请注意查收手机短信。";
    }else{
       _detailView.lblResultTip.text=[NSString stringWithFormat:@"预订失败，%@",[dictReturn objectForKey:@"rspDesc" ]];
    }
    
    _detailView.btnSubmitOrder.enabled=YES;
    _detailView.btnSubmitOrder.titleLabel.text=@"立即预订";
    [self stopActivityView];
}
-(void)createActivityIndicatorView{
    _activityView=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
    CGRect activityFrame=_activityView.frame;
    activityFrame.origin.x=_detailView.frame.size.width/2-50;
    activityFrame.origin.y=_detailView.frame.size.height/2;
    
    [_activityView setFrame:activityFrame];
    
    _activityView.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;
    
    
    
    _loadMoreTip=[[UILabel alloc] initWithFrame:CGRectMake(activityFrame.origin.x+30, activityFrame.origin.y+5, 100, 20)];
    
    _loadMoreTip.textAlignment=NSTextAlignmentCenter;
    
    _loadMoreTip.text=@"正在处理...";
    _loadMoreTip.textColor=[UIColor redColor];
    _loadMoreTip.font=[UIFont systemFontOfSize:16];
    _loadMoreTip.backgroundColor=[UIColor clearColor];
    
    [_detailView addSubview:_activityView];
    [_detailView addSubview:_loadMoreTip];
    [_activityView startAnimating];
    _activityView.hidden=NO;
    _loadMoreTip.hidden=NO;
}
-(void)stopActivityView{
    [_activityView stopAnimating];
    _activityView.hidden=YES;
    _loadMoreTip.hidden=YES;
}
-(void)loadTicketData{
    if(self.dictScenery==nil)return ;
    _sceneryId=[self.dictScenery objectForKey:@"sceneryId"];
    NSString* xmlData=[LocalTravelRequest getSceneryPrice:_sceneryId showDetail:1 useCache:0];
    
    //NSLog(@"%@",xmlData);
    SceneryPriceParse* parse=[[SceneryPriceParse alloc]initWithXmlData:xmlData];
      _dictAllPolicy=[parse getScenaryPriceList];
    _priceArray=[_dictAllPolicy objectForKey:@"policy"];
    parse=nil;
    xmlData=nil;
    
    [_tableTicket reloadData];

  //  NSIndexPath* indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
    [_tableTicket selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionBottom];
    
    
    NSString* useCard=[_dictAllPolicy objectForKey:@"useCard"];
     
    if(useCard !=nil && [useCard isEqualToString:@"1"]){
        
        [_detailView calButtonOrderFrame:YES];
    }else{
       [_detailView calButtonOrderFrame:NO];
        
    
    }

    
    [self showTicketDetail:0];
   
    
    [self stopActivityView];
}

//显示指定的门票信息
-(void)showTicketDetail:(int)index{
    if(_priceArray !=nil && _priceArray.count>=index){
        _selectedTicketDict=[_priceArray objectAtIndex:index];
        
        [_detailView showTicketDetail:_selectedTicketDict otherDict:_dictAllPolicy];
        /*
        _detailView.lblTicketName.text=[_selectedTicketDict objectForKey:@"policyName"];
        
      _detailView.lblDescription.text=[NSString stringWithFormat:@"说明：\r\n\r\n%@ \r\n \r\n取票地点：\r\n\r\n%@",[_selectedTicketDict objectForKey:@"remark"],[_selectedTicketDict objectForKey:@"gMode"]];
        _detailView.lblPrice.text=[NSString stringWithFormat:@"¥：%@",[_selectedTicketDict objectForKey:@"tcPrice"] ];
        _detailView.doublePrice=[[_selectedTicketDict objectForKey:@"tcPrice"] doubleValue ];
        double count=[_detailView.textFieldCount.text doubleValue];
        
        
        _detailView.lblPriceTotal.text=[NSString stringWithFormat:@"总额：%.2f",count * _detailView.doublePrice];
         */
    }

}
#pragma mark - Table view data source
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 113;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(_priceArray !=nil) return _priceArray.count;
    
    return 0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(_priceArray !=nil && _priceArray.count>=indexPath.row){
        
        [self showTicketDetail:indexPath.row];
    }

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TicketTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TicketTableCell" forIndexPath:indexPath];
    
    if(_priceArray !=nil && _priceArray.count>indexPath.row){
        NSDictionary* dict=[_priceArray objectAtIndex:indexPath.row];
        
        cell.lblPolicyName.text=[dict objectForKey:@"policyName"];
        
        //cell.lblPrice.isWithStrikeThrough=YES;
        cell.lblPrice.text=[NSString stringWithFormat:@"票面价：%@",[dict objectForKey:@"price"]];
        cell.lblTCPrice.textColor=[UIColor redColor];
        cell.lblTCPrice.text=[NSString stringWithFormat:@"同程价：%@",[dict objectForKey:@"tcPrice"]];
        NSString* pMode=[dict objectForKey:@"pMode"];
        if(pMode !=nil){
            if([pMode isEqualToString:@"0"]){
                cell.lblPayMode.text=@"景区现付";
            }else if([pMode isEqualToString:@"1"]){
                cell.lblPayMode.text=@"在线支付";
            }else{
                cell.lblPayMode.text=@"其他方式";
            }
        }
    }
    
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    
    UILabel* lblHeader=[[UILabel alloc] init];
    lblHeader.font=[UIFont systemFontOfSize:18];
    lblHeader.textColor=[UIColor blueColor];
    lblHeader.backgroundColor=[UIColor colorWithRed:192.0/255.0f green:192.0f/255.0f blue:192.0f/255.0f alpha:0.5];
    lblHeader.text=@"门票";
    return lblHeader;
    
}
-(void)dealloc{
    _tableTicket=nil;
    
      _selectedTicketDict=nil;
      _detailView=nil;
     _activityView=nil;
     _loadMoreTip=nil;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
